<template>
  <div class="char">
      <ul>
          <li class="char-item" v-for="(item, key) of citys" :key="key" @click="handleCharClick(key)"
          @touchstart="handleTouchStart"
          @touchmove="handleTouchMove"
          @touchend="handleTouchEnd"
          :ref="key"
          >{{ key }}</li>
      </ul>
  </div>
</template>

<script>
export default {
  name: 'CityChar',
  props: {
    citys: Object
  },
  data () {
    return {
      flag: false,
      firstToTop: 0,
      timer: null
    }
  },
  computed: {
    letter () {
      let letters = []
      for (let key in this.citys) {
        letters.push(key)
      }
      return letters
    }
  },
  methods: {
    handleCharClick (char) {
      this.$emit('charClick', char)
    },
    handleTouchStart () {
      this.flag = true
    },
    handleTouchMove (e) {
      if (this.flag) {
        if (this.timer) {
          clearTimeout(this.timer)
        } else {
          setTimeout(() => {
            let toucheTop = e.touches[0].clientY - 79
            let index = Math.floor((toucheTop - this.firstToTop) / 17)
            this.$emit('charClick', this.letter[index])
          }, 16)
        }
      }
    },
    handleTouchEnd () {
      this.flag = false
    }
  },
  updated () {
    this.firstToTop = this.$refs['A'][0].offsetTop
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl'
    .char
        position absolute
        right 0
        top 1.56rem
        bottom 0
        display flex
        flex-direction column
        justify-content center
        .char-item
            color $bgColor
            padding .03rem .2rem
</style>
